package club.itstudio.IContainer.controller.student;

import club.itstudio.IContainer.annotation.LoginRequired;
import club.itstudio.IContainer.annotation.StudentRequired;
import club.itstudio.IContainer.entity.User;
import club.itstudio.IContainer.service.AuthService;
import club.itstudio.IContainer.service.StudentCourseApplicationService;
import club.itstudio.IContainer.util.Result;
import club.itstudio.IContainer.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/student/application")
public class StudentApplicationController {

    private StudentCourseApplicationService studentCourseApplicationService;
    private JdbcTemplate jdbcTemplate;
    private AuthService authService;

    @Autowired
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Autowired
    public void setAuthService(AuthService authService) {
        this.authService = authService;
    }

    @Autowired
    public void setStudentCourseApplicationService(StudentCourseApplicationService studentCourseApplicationService) {
        this.studentCourseApplicationService = studentCourseApplicationService;
    }

    @GetMapping
    @LoginRequired
    @StudentRequired
    public Result getApplications(@RequestParam int page, @RequestParam int limit) {
        User student = authService.threadLocalUsers.get();
        String sql= "SELECT\n" +
                "\tstudent_course_application_list.*,\n" +
                "\t`user`.username \n" +
                "FROM\n" +
                "\t(\n" +
                "\tSELECT\n" +
                "\t\tstudent_course_application.*,\n" +
                "\t\tcourse.course_name,\n" +
                "\t\tcourse.description,\n" +
                "\t\tcourse.created_time,\n" +
                "\t\tcourse.teacher_id \n" +
                "\tFROM\n" +
                "\t\tstudent_course_application\n" +
                "\t\tJOIN course ON student_course_application.course_id = course.id \n" +
                "\tWHERE\n" +
                "\t\tstudent_course_application.user_id = ? \n" +
                "\t) AS student_course_application_list\n" +
                "\tJOIN `user` ON student_course_application_list.teacher_id = `user`.id \n" +
                "ORDER BY\n" +
                "\tid DESC \n" +
                "\tLIMIT ?,?";
        List<HashMap<String, Object>> data = jdbcTemplate.query(sql, (RowMapper) (resultSet, i) -> {
            HashMap<String, Object> map = new HashMap<>();
            map.put("id", resultSet.getInt("id"));
            map.put("user_id", resultSet.getInt("user_id"));
            map.put("course_id", resultSet.getInt("course_id"));
            map.put("status", resultSet.getInt("status"));
            map.put("courseName", resultSet.getString("course_name"));
            map.put("description", resultSet.getString("description"));
            map.put("createdTime", resultSet.getLong("created_time"));
            map.put("teacher_id", resultSet.getInt("teacher_id"));
            map.put("teacher_name", resultSet.getString("username"));
            return map;
        }, student.id, (page - 1) * limit, limit);

        int count = jdbcTemplate.queryForObject("select count(*) from student_course_application where user_id=?", Integer.class, student.id);
        HashMap<String, Object> result = new HashMap<>();
        result.put("content", data);
        result.put("total", count);
        return ResultUtil.success(result);
    }
}
